define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'editable'], function ($, undefined, Backend, Table, Form, undefined) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'dahui/zfbt/index',
                    add_url: 'dahui/zfbt/add',
                    edit_url: 'dahui/zfbt/edit',
                    del_url: 'dahui/zfbt/del',
                    multi_url: 'dahui/zfbt/multi',
                    import_url: 'dahui/zfbt/import',
                    table: 'dahui_zfbt',
                }
            });

            var table = $("#table");
            console.log("audit = " + Config.audit);

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                showToggle: false,
                search: false,
                sortName: 'id',
                searchFormVisible: true,
                exportTypes: ['csv'],
                dblClickToEdit: false, //是否启用双击编辑
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate:false},
                        {field: 'djdate', title: __('Djdate'), operate:false},
                        {field: 'tbstate', title: __('Tbstate'), searchList: {"":__(''),"重复":__('重复')}, formatter: Table.api.formatter.normal},
                        {field: 'taobaoname', title: __('Taobaoname'), sortable: true, operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        {field: 'zfbname', title: __('Zfbname'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        {field: 'zname', title: __('Zname'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        {field: 'ykje', title: __('Ykje'), operate:false},
                        {field: 'tkdate', title: __('Tkdate'), operate:false},
                        {
                            field: 'remark',
                            title: __('备注'),
                            editable: {
                                emptytext: '无',
                                rows:10,
                                tpl: "<textarea rows='10' cols='50'></textarea>",
                                disabled: !Config.audit , //true 则不能编辑 ---- 操作权限
                                success: function (response, newValue) {
                                    //$(".btn-refresh").trigger("click");
                                }
                            },
                            operate: false
                        },
                        {
                            field: 'qita',
                            title: __('其他'),
                            editable: {
                                emptytext: '无',
                                rows:10,
                                tpl: "<textarea rows='10' cols='50'></textarea>",
                                disabled: !Config.audit , //true 则不能编辑 ---- 操作权限
                                success: function (response, newValue) {
                                    //$(".btn-refresh").trigger("click");
                                }
                            },
                            operate: false
                        },
                        {field: 'refundornot', title: __('Refundornot'), searchList: {"已退款":__('已退款'),"待退款":__('待退款'),"":__('')}, formatter: Controller.api.formatter.tkstatus},
                        {field: 'djman', title: __('Djman'), operate:false},
                        //{field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            //点击退款状态
            $(document).on("dblclick", ".tkstatus[data-id]", function () {

                if (Config.audit) {
                    var url = 'dahui/zfbt/changetstatus';
                    var id = $(this).data('id');
                    var _this = $(this);
                    Fast.api.ajax({
                        url: url,
                        data: {ids: id},
                    }, function (data, ret) {
                        console.log(ret.data.refundornot);
                        _this.html(ret.data.refundornot);
                        if (ret.data.refundornot != '已退款') {
                            _this.addClass('text-danger');
                        } else {
                            _this.removeClass('text-danger')
                        }
                        if (ret.data.refundornot == '已退款') {
                            //$(".btn-refresh").trigger("click");
                        }
                        //return false;
                    });
                } else if (Config.tuikuan) {
                    var url = 'dahui/zfbt/changetstatus4';
                    var id = $(this).data('id');
                    var _this = $(this);
                    Fast.api.ajax({
                        url: url,
                        data: {ids: id},
                    }, function (data, ret) {
                        console.log(ret.data.refundornot);
                        _this.html(ret.data.refundornot);
                        if (ret.data.refundornot != '已退款') {
                            _this.addClass('text-danger');
                        } else {
                            _this.removeClass('text-danger')
                        }
                        if (ret.data.refundornot == '已退款') {
                            //$(".btn-refresh").trigger("click");
                        }
                        //return false;
                    });
                }
                return false;
            });

            // 检查身份证
            $(document).on("click", ".btn-jhtb", function () {
                if (Config.audit) {
                    var url = 'dahui/zfbt/jhtb';

                    var that = this;
                    Layer.confirm(__('校核淘宝账号重复项?'), {icon: 3, title: '提示'}, function (index) {
                        Backend.api.ajax({
                            url: url,
                        }, function () {
                            Layer.close(index);
                            $(".btn-refresh").trigger("click");
                        });
                    });
                }
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                tkstatus: function (value, row, index) {
                    if (row.refundornot != '已退款') {
                        return [
                            '<a href="javascript:;" class="tkstatus text-danger" data-id= "' + row.id + '" > ' + value + '</a>'
                        ].join("")

                    } else {
                        return [
                            '<a href="javascript:;" class="tkstatus" data-id= "' + row.id + '" > 已退款 </a>'
                        ].join("")
                    }
                }
            }
        }
    };
    return Controller;
});